package rs.ac.uns.ftn.informatika.mbs2.e11031.etaksi.servlet;

import java.io.IOException;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import rs.ac.uns.ftn.informatika.mbs2.e11031.etaksi.dao.PutnikDao;
import rs.ac.uns.ftn.informatika.mbs2.e11031.etaksi.dao.VozacDao;
import rs.ac.uns.ftn.informatika.mbs2.e11031.etaksi.entitet.Putnik;
import rs.ac.uns.ftn.informatika.mbs2.e11031.etaksi.entitet.Vozac;

@SuppressWarnings("serial")
public class RegistracijaKontroler extends HttpServlet {

	@EJB
	private PutnikDao cPutnikDao;

	@EJB
	private VozacDao cVozacDao;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		int vrstaKorisnika = Integer.parseInt(req
				.getParameter("vrstaKorisnika"));
		String korisnickoIme = req.getParameter("korisnickoIme");
		String lozinka = req.getParameter("lozinka1");
		long jmbg = Long.parseLong(req.getParameter("jmbg"));
		String ime = req.getParameter("ime");
		String prezime = req.getParameter("prezime");

		if (vrstaKorisnika == 1) {
			Putnik putnik = new Putnik(jmbg, ime, prezime, korisnickoIme,
					lozinka);
			cPutnikDao.sacuvaj(putnik);
		} else {
			Vozac vozac = new Vozac(jmbg, ime, prezime, korisnickoIme, lozinka);
			cVozacDao.sacuvaj(vozac);
		}

		getServletContext().getRequestDispatcher("/jsp/prijava.jsp").forward(
				req, resp);
	}
}
